﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NWN.GDY.Service.Entity;
using NWN.GDY.Service.Common;
using Sdo.Payment.Common;

namespace NWN.GDY.Service.Core
{
    public class GDYEngine
    {
        #region 应用接口
        /// <summary>
        /// 新建一桌
        /// </summary>
        /// <returns>TableID</returns>
        public string NewTable(string passport, string nick, string uid, string tablename)
        {
            try
            {
                string sOut= new TableBC().CreateNewTable(passport, nick, uid, tablename);
                Log.Info("NewTable_" + passport + "_" + nick + "_" + uid + "_" + tablename + "_result_" + sOut);
                return sOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return "";
            }
        }

        /// <summary>
        /// 取房信息
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <returns>Room</returns>
        public Room GetRoom(string roomGUID)
        {
            try
            {
                Room tOut = new TableBC().GetRoom(roomGUID);
                Log.Info("GetRoom_" + roomGUID + "_result_" + (tOut == null ? "" : XmlConfigMgr.ToXml(tOut)));
                return tOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return null;
            }
        }

        /// <summary>
        /// 占坐
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <param name="player">玩家信息</param>
        /// <returns></returns>
        public int OcuppySeat(string roomGUID, Player player)
        {
            try
            {
                int iOut = new TableBC().OcuppySeat(roomGUID, player);
                Log.Info("OcuppySeat_" + roomGUID + "_" + player.NickName + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 切换坐位状态
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <param name="seatID">位子编号</param>
        /// <returns>code</returns>
        public int ToggleSeatDisable(string roomGUID, int seatID, string commanduid, out bool seatDisabled)
        {
            try
            {
                int iOut = new TableBC().ToggleSeatDisable(roomGUID, seatID, commanduid, out seatDisabled);
                Log.Info("ToggleSeatDisable_" + roomGUID + "_" + seatID.ToString() + "_" + commanduid + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                seatDisabled = false;
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 准备好了
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <param name="playerID">玩家编号</param>
        /// <returns>code</returns>
        public int PlayerReady(string roomGUID, string playerID)
        {
            try
            {
                int iOut = new TableBC().PlayerReady(roomGUID, playerID);
                Log.Info("PlayerReady_" + roomGUID + "_" + playerID + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="uid">用户名</param>
        /// <param name="psd">密码</param>
        /// <returns>code</returns>
        public int RegisterUser(string uid, string psd)
        {
            try
            {
                int iOut = new UserBC().RegisterUser(uid, psd);
                Log.Info("RegisterUser_" + uid + "_" + psd + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="uid">用户名</param>
        /// <param name="psd">密码</param>
        /// <returns>code</returns>
        public int UserLogin(string uid, string psd)
        {
            try
            {
                int iOut = new UserBC().UserLogin(uid, psd);
                Log.Info("UserLogin_" + uid + "_" + psd + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 取房间列表
        /// </summary>
        /// <param name="RoomType">房间类型(新手房,比赛房,玩家房等)</param>
        /// <returns>List<Room></returns>
        public List<Room> GetRooms(int RoomType)
        {
            try
            {
                return new RoomBC().GetRooms(RoomType);
            }
            catch (Exception x)
            {
                Log.Error(x);
                return null;
            }
        }

        /// <summary>
        /// 进入房间
        /// </summary>
        /// <param name="uid">用户名</param>
        /// <param name="passport">密码</param>
        /// <param name="roomguid">房间ID</param>
        /// <returns>code</returns>
        public int EnterRoom(string uid, string passport, string roomguid)
        {
            try
            {
                int iOut = new RoomBC().EnterRoom(uid, passport, roomguid);
                Log.Info("EnterRoom_" + uid + "_" + passport + "_" + roomguid + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 取房信息
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <param name="userid">用户</param>
        /// <param name="recordid">出牌记录ID</param>
        /// <param name="vplayrecord">出牌记录版本</param>
        /// <param name="vroom">房间版本</param>
        /// <param name="vround">局版本</param>
        /// <param name="vucoin">游戏币版本</param>
        /// <param name="vuroom">人员版本</param>
        /// <param name="vuinfo">用户信息版本</param>
        /// <param name="chatid">聊天记录ID</param>
        /// <param name="vchat">聊天记录版本</param>
        /// <param name="vadminroom">房管理员版本</param>
        /// <returns>Room</returns>
        public Room GetRoom(string RoomGUID, string userid, long recordid, string vplayrecord, string vroom, string vround, string vucoin, string vuroom, string vuinfo, long chatid, string vchat, string vadminroom, string vroundresult)
        {
            try
            {
                Room room = new RoomBC().GetRoom(RoomGUID, userid, recordid, vplayrecord, vroom, vround, vucoin, vuroom, vuinfo, chatid, vchat, vadminroom, vroundresult);
                Log.Info("GetRoom_" + RoomGUID + "_" + userid + "_" + recordid.ToString() + "_" + vplayrecord + "_" + vroom + "_" + vround + "_" + vucoin + "_" + vuroom + "_" + vuinfo + "_" + chatid.ToString() + "_" + vchat + "_" + vadminroom + "_" + vroundresult + "_result_" + room.Result.ToString());
                return room;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return new Room() { Result = GDYConsts.ReturnDefine.Exception };
            }
        }

        /// <summary>
        /// 坐下
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <param name="SeatID">坐位号</param>
        /// <param name="userid">用户</param>
        public int Sit(string uid, string roomguid, int SeatID)
        {
            try
            {
                int iOut = new RoomBC().Sit(uid,roomguid, SeatID);
                Log.Info("Sit_" + roomguid + "_" + uid + "_" + SeatID.ToString() + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 站起
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <param name="userid">用户</param>
        public int Stand(string uid, string roomguid)
        {
            try
            {
                int iOut = new RoomBC().Stand(uid, roomguid);
                Log.Info("Stand_" + roomguid + "_" + uid + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 准备
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <param name="userid">用户</param>
        public int Ready(string uid, string roomguid)
        {
            try
            {
                int iOut = new RoomBC().Ready(uid, roomguid);
                Log.Info("Ready_" + roomguid + "_" + uid + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 过牌
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <param name="roundguid">局编号</param>
        /// <param name="uid">用户</param>
        public int Pass(string uid, string roomguid, string roundguid)
        {
            try
            {
                int iOut = new RoomBC().Pass(uid, roomguid, roundguid);
                Log.Info("Pass_" + roomguid + "_" + uid + "_" + roundguid + "_result_" + iOut.ToString());
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 聊天
        /// </summary>
        /// <param name="roomGUID">房编号</param>
        /// <param name="chatcontent">聊天内容</param>
        /// <param name="uid">用户</param>
        public int Chat(string uid, string roomguid, string chatcontent)
        {
            try
            {
                int iOut = new RoomBC().Chat(uid, roomguid, chatcontent);
                return iOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception;
            }
        }

        /// <summary>
        /// 出牌
        /// </summary>
        /// <param name="userid">用户</param>
        /// <param name="CardID">出的牌</param>
        /// <param name="ArrageCardID">多选一的牌</param>
        /// <param name="roomGUID">房编号</param>
        /// <param name="RoundGUID">局编号</param>
        /// <param name="lastcardids">上一手牌</param>
        /// <param name="lasttype">上一手牌型</param>
        public string Discard(string UserID, string CardID, string ArrageCardID, string roomGUID, string RoundGUID, string lastcardids, int lasttype)
        {
            try
            {
                string sOut = new RoomBC().Discard(UserID, CardID, ArrageCardID, roomGUID, RoundGUID, lastcardids, lasttype);
                Log.Info("Discard_" + UserID + "_" + CardID + "_" + ArrageCardID + "_" + roomGUID + "_" + RoundGUID + "_" + lastcardids + "_" + lasttype.ToString() + "_result_" + sOut);
                return sOut;
            }
            catch (Exception x)
            {
                Log.Error(x);
                return GDYConsts.ReturnDefine.Exception.ToString();
            }
        }

        #endregion

        #region 管理接口
        public List<Room> GetALLTables()
        {
            try
            {
                Log.Info("GetALLTables");
                return new TableBC().GetALLTables();
            }
            catch (Exception x)
            {
                Log.Error(x);
                return null;
            }
        }

        public void FinishTable(string guid)
        {
            try
            {
                Log.Info("FinishTable");
                new TableBC().FinishTable(guid);
            }
            catch (Exception x)
            {
                Log.Error(x);
            }
        }

        public void FinishAllTable()
        {
            try
            {
                Log.Info("FinishAllTable");
                new TableBC().FinishAllTable();
            }
            catch (Exception x)
            {
                Log.Error(x);
            }
        }
        #endregion

    }
}
